package com.unibuy.smartdevice;

import static android.view.View.VISIBLE;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.unibuy.smartdevice.databinding.ActivityHomeBinding;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.exception.ErrorCode;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.exception.LogsFileNullException;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.ui.FontendActivity;
import com.unibuy.smartdevice.ui.tools.ImageGlide;
import com.unibuy.smartdevice.ui.tools.VideoEoxPlayer;

public class HomeActivity extends AppCompatActivityAbstract {

    private ActivityHomeBinding binding;
    private static VideoEoxPlayer videoEoxPlayer;

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTools().gotoActivity(FontendActivity.class);
                finish();
            }
        });

        startOnSwitchFragment();
    }

    public void startOnSwitchFragment() {
        try {
            if (videoEoxPlayer != null) {
                videoEoxPlayer.videoStop(); // 確保舊的播放停止
            }

            // **確保 ExoPlayer 已完全釋放**
            Thread.sleep(100);

            if (MyApp.getInstance().getFontendAdUrlString().size() == 1) {
                String mediaUri = MyApp.getInstance().getFontendAdUrlString().get(0);
                boolean isImage = mediaUri.endsWith(".png") || mediaUri.endsWith(".jpg") || mediaUri.endsWith(".gif");
                if (isImage) {
                    videoEoxPlayer = null;
                    binding.videoView.setVisibility(View.GONE);
                    binding.imageAd.setVisibility(VISIBLE);

                    Glide.with(getCtx()).load(mediaUri)
                            .placeholder(R.drawable.loading)
                            .error(R.drawable.loading)
                            .transform(new ImageGlide.WhiteTintTransformation(128))
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                            .into(binding.imageAd);
                }
            } else {
                binding.videoView.setVisibility(VISIBLE);
                binding.imageAd.setVisibility(View.GONE);

                videoEoxPlayer = new VideoEoxPlayer(getCtx()); // 重新初始化播放器
                videoEoxPlayer.videoPlay(binding.videoView, SlotField.VMC.getField()); // 確保 PlayerView 綁定
            }
        } catch (Exception e) {
            getLogs().warning(e);
        }
    }

    public void stopOnSwitchFragment() {
        if (videoEoxPlayer != null) {
            videoEoxPlayer.videoStop();
            videoEoxPlayer = null;  // 確保釋放，避免未來 `videoPlay()` 重新建立失敗
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopOnSwitchFragment();
    }
}
